# 源文件收集
set(LIB_SOURCES
    src/BackendFactory.cpp
    
    src/Log4cplusBackend.cpp
    src/LogStyle.cpp
    src/LogWnd.cpp
    src/Logger.cpp
    src/SpdlogBackend.cpp
    src/LogProcessor.cpp
)
file(GLOB_RECURSE LIB_HEADERS include/*.h)

# 创建库目标
# add_library(libunits STATIC ${LIB_SOURCES})
add_library(libunits ${LIB_SOURCES})
add_library(libunits::libunits ALIAS libunits)

# 包含目录
target_include_directories(libunits PRIVATE
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
    $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>

    # 第三方units头文件（关键添加）
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../3rdParty/units>
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../3rdParty/libconfig/lib>

    # 私有头文件（如有需要）
    PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src
)

# 链接依赖
target_link_libraries(libunits PUBLIC
    units::units
    libconfig++
)

# 安装规则
install(TARGETS libunits
    EXPORT libunitsTargets
    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
    INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)

install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})

option(BUILD_SHARED_LIBS "Build as shared library" OFF)

if(NOT BUILD_SHARED_LIBS)
    # 静态库不需要dllimport/dllexport
    target_compile_definitions(libunits PRIVATE UNITS_STATIC_DEFINE)
    target_compile_definitions(libunits PRIVATE UNITS_STATIC_DEFINE)
endif()

# 导出配置
install(EXPORT libunitsTargets
    FILE libunitsTargets.cmake
    NAMESPACE libunits::
    DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/libunits
)
